package xyz.labmem.base.sys.module.log.controller

import cn.hutool.core.util.StrUtil
import xyz.labmem.base.sys.base.BaseController
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import xyz.labmem.base.core.result.RP
import xyz.labmem.base.core.base.BaseVerify
import xyz.labmem.base.core.tool.isNotBlank
import xyz.labmem.base.sys.consts.PathPrefixConst
import xyz.labmem.base.sys.module.log.controller.request.SysOpLogPageReq
import xyz.labmem.base.sys.module.log.controller.request.SysVisLogPageReq
import xyz.labmem.base.sys.module.log.entity.SysOPLog
import xyz.labmem.base.sys.module.log.entity.SysVISLog
import xyz.labmem.base.sys.module.log.mapper.SysOPLogMapper
import xyz.labmem.base.sys.module.log.mapper.SysVISLogMapper

/**
 * @Author lty
 * @Date 2021/12/17 9:21
 */
@Api(tags = ["日志接口"])
@RestController
@RequestMapping("${PathPrefixConst.SYS_PATH_PREFIX}log")
class SysLogController(
    private var sysOPLogMapper: SysOPLogMapper,
    private var sysVISLogMapper: SysVISLogMapper,
) : BaseController() {

    @PostMapping("opLogPage")
    @ApiOperation(value = "操作日志分页")
    fun opLogPage(@RequestBody @Validated(BaseVerify.page::class) req: SysOpLogPageReq): RP<List<SysOPLog>> {
        return page(
            sysOPLogMapper.selectPage(
                req.IPage(),
                KtQueryWrapper(SysOPLog::class.java)
                    .like(req.name.isNotBlank(), SysOPLog::name, req.name)
                    .eq(req.opType.isNotBlank(), SysOPLog::opType, req.opType)
                    .eq(req.success != null, SysOPLog::success, req.success)
                    .like(req.ip.isNotBlank(), SysOPLog::ip, req.ip)
                    .like(req.account.isNotBlank(), SysOPLog::account, req.account)
                    .ge(req.BTime != null, SysOPLog::createTime, req.BTime)
                    .le(req.ETime != null, SysOPLog::createTime, req.ETime)
                    .orderByDesc(SysOPLog::createTime)
            )
        )
    }

    @PostMapping("visLogPage")
    @ApiOperation(value = "访问日志分页")
    fun visLogPage(@RequestBody @Validated(BaseVerify.page::class) req: SysVisLogPageReq): RP<List<SysVISLog>> {
        return page(
            sysVISLogMapper.selectPage(
                req.IPage(),
                KtQueryWrapper(SysVISLog::class.java)
                    .like(req.name.isNotBlank(), SysVISLog::name, req.name)
                    .eq(req.success != null, SysVISLog::success, req.success)
                    .like(req.ip.isNotBlank(), SysVISLog::ip, req.ip)
                    .like(req.account.isNotBlank(), SysVISLog::account, req.account)
                    .ge(req.BTime != null, SysVISLog::createTime, req.BTime)
                    .le(req.ETime != null, SysVISLog::createTime, req.ETime)
                    .orderByDesc(SysVISLog::createTime)
            )
        )
    }

}